package com.scrm.server.wx.cp.web;

import com.scrm.common.exception.BaseException;
import com.scrm.common.log.annotation.Log;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scrm.api.wx.cp.dto.FissionExpandDTO;
import com.scrm.api.wx.cp.dto.WxFissionTaskPageDTO;
import com.scrm.api.wx.cp.dto.WxFissionTaskSaveDTO;
import com.scrm.api.wx.cp.dto.WxFissionTaskUpdateDTO;
import com.scrm.api.wx.cp.entity.WxFissionTask;
import com.scrm.api.wx.cp.vo.*;
import com.scrm.common.constant.R;
import com.scrm.server.wx.cp.service.IWxFissionContactService;
import com.scrm.server.wx.cp.service.IWxFissionCustomerService;
import com.scrm.server.wx.cp.service.IWxFissionTaskCustomerDetailService;
import com.scrm.server.wx.cp.service.IWxFissionTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 企微应用宝-裂变任务信息 控制器
 * @author xxh
 * @since 2022-03-21
 */
@RestController
@RequestMapping("/wxFissionTask")
@Api(tags = {"企微应用宝-裂变任务信息"})
public class WxFissionTaskController {

    @Autowired
    private IWxFissionTaskService wxFissionTaskService;

    @Autowired
    private IWxFissionCustomerService fissionCustomerService;

    @Autowired
    private IWxFissionContactService fissionContactService;

    @Autowired
    private IWxFissionTaskCustomerDetailService fissionTaskCustomerDetailService;


    @PostMapping("/pageList")
    @ApiOperation(value = "分页查询")
    @Log(modelName = "企微应用宝-裂变任务信息", operatorType = "分页查询")
    public R<IPage<WxFissionTaskVO>> pageList(@Validated @RequestBody WxFissionTaskPageDTO dto){
        return R.data(wxFissionTaskService.pageList(dto));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据主键查询")
    @Log(modelName = "企微应用宝-裂变任务信息", operatorType = "根据主键查询")
    public R<WxFissionTask> findById(@PathVariable(value = "id") String id){
        Assert.isTrue(StringUtils.isNotBlank(id), "企微应用宝-裂变任务信息ID不能为空");
        return R.data(wxFissionTaskService.findById(id));
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "新增")
    @Log(modelName = "企微应用宝-裂变任务信息", operatorType = "新增")
    public R<WxFissionTask> save(@RequestBody @Valid WxFissionTaskSaveDTO dto){
        return R.data(wxFissionTaskService.save(dto));
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "修改")
    @Log(modelName = "企微应用宝-裂变任务信息", operatorType = "修改")
    public R<WxFissionTask> update(@RequestBody @Valid WxFissionTaskUpdateDTO dto){
        return R.data(wxFissionTaskService.update(dto));
    }

    @GetMapping("/delete")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "string", paramType = "query")
    @ApiOperation(value = "删除")
    @Log(modelName = "企微应用宝-裂变任务信息", operatorType = "删除")
    public R<Void> delete(@RequestParam String id){
        Assert.isTrue(StringUtils.isNotBlank(id), "企微应用宝-裂变任务信息ID不能为空");
        wxFissionTaskService.delete(id);
        return R.success("删除成功");
    }

    @GetMapping("/over")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "string", paramType = "query")
    @ApiOperation(value = "结束")
    @Log(modelName = "企微应用宝-裂变任务信息", operatorType = "结束")
    public R<Void> over(@RequestParam String id){
        Assert.isTrue(StringUtils.isNotBlank(id), "企微应用宝-裂变任务信息ID不能为空");
        wxFissionTaskService.over(id);
        return R.success("删除成功");
    }

    @PostMapping("/countByCondition")
    @ApiOperation(value = "（活动数据）根据条件统计")
    @Log(modelName = "企微应用宝-裂变任务信息", operatorType = "（活动数据）根据条件统计")
    public R<WxFissionTaskCountVO> countByCondition(@RequestBody @Valid WxFissionCountConditionVO conditionVO){
        return R.data(wxFissionTaskService.countByCondition(conditionVO));
    }

    @PostMapping("/findCountList")
    @ApiOperation(value = "（活动数据）查询活动数据列表")
    @Log(modelName = "企微应用宝-裂变任务信息", operatorType = "（活动数据）查询活动数据列表")
    public R<IPage<WxFissionContactVO>> findCountList(@RequestBody @Valid WxFissionContactQueryVO wxFissionContactQueryVO){
        return R.data(fissionContactService.findCountList(wxFissionContactQueryVO));
    }

    @PostMapping("/getInviteDetails")
    @ApiOperation(value = "（活动数据）查询邀请详情列表的数据（id传那个extCustomerId）")
    @Log(modelName = "企微应用宝-裂变任务信息", operatorType = "（活动数据）查询邀请详情列表的数据（id传那个extCustomerId）")
    public R<IPage<WxFissionCustomerVO>> getInviteDetails(@RequestBody @Valid FissionInviteConditionVO conditionVO){
        if (StringUtils.isBlank(conditionVO.getExtCorpId())) {
            throw new BaseException("企业id必填！");
        }
        return R.data(fissionCustomerService.getInviteDetails(conditionVO));
    }

    @PostMapping("/expand")
    @ApiOperation(value = "推广")
    @Log(modelName = "企微应用宝-裂变任务信息", operatorType = "推广")
    public R expand(@RequestBody @Valid FissionExpandDTO dto){
        wxFissionTaskService.expand(dto);
        return R.success("推广成功");
    }

    @PostMapping("/price")
    @ApiOperation(value = "兑奖")
    @Log(modelName = "企微应用宝-裂变任务信息", operatorType = "兑奖")
    public R price(@RequestBody @Valid IdVO dto){
        fissionTaskCustomerDetailService.price(dto);
        return R.success("兑奖成功");
    }
}
